package cn.jingzhuan.fm.utils

import android.app.Activity
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Handler
import android.text.TextUtils
import android.widget.ImageView
import cn.jingzhuan.fm.R
import cn.jingzhuan.fm.ui.GlideApp
import com.bumptech.glide.GenericTransitionOptions
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation
import com.bumptech.glide.request.RequestOptions
import java.util.concurrent.Executors


class ImageLoader {

    companion object {
        var imageThreadPool = Executors.newSingleThreadExecutor()

        fun loadImage(imageView: ImageView, url: String?) {
            if (imageView.context is Activity) {
                if ((imageView.context as Activity).isFinishing) return
            }
            GlideApp.with(imageView.context).load(url)
                    .placeholder(ColorDrawable(Color.GRAY))
                    .centerCrop()
                    .into(imageView)
        }

        fun loadImage(imageView: ImageView, url:String?,tranform: BitmapTransformation) {
            GlideApp.with(imageView.context).load(url)
                    .placeholder(ColorDrawable(Color.GRAY))
                    .transform(tranform)
                    .into(imageView)
        }

        fun loadAvatar(imageView: ImageView, url: String?) {
            if (TextUtils.isEmpty(url)) {
                imageView.setImageResource(R.drawable.ico_default_avatar)
                return
            }

            if (imageView.context is Activity) {
                if ((imageView.context as Activity).isFinishing) return
            }
            Glide.with(imageView.context).load(url)
//                    .placeholder(R.drawable.icon_default_avatar)
//                    .error(R.drawable.icon_default_avatar)
//                    .dontAnimate()
//                    .bitmapTransform(CropCircleTransformation(imageView.context))
                    .into(imageView)
        }

        fun loadBlurImage(imageView: ImageView,url: String?){
            val futureTarget = Glide.with(imageView.context)
                    .asBitmap()
                    .load(url)
                    .submit()
            var handler = Handler()
            imageThreadPool.execute {
                val bitmap = futureTarget.get()
                handler.post {
                    BlurImageUtil.blur(bitmap,imageView)
                }
            }
        }

        fun loadCircleImage(imageView: ImageView, url: String?) {
//            if (url == null || url.isEmpty()) {
//                imageView.setImageDrawable(ContextCompat.getDrawable(imageView.context, placeHolder))
//                return
//            }
            Glide.with(imageView.context)
                    .load(url)
                    .transition(GenericTransitionOptions.with(android.R.anim.fade_in))
                    .apply(RequestOptions()
                            .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                            .placeholder(ColorDrawable(Color.GRAY))
                            .circleCrop())
                    .into(imageView)
        }

    }

}
